package com.mid;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lxk on 2019/11/30.
 */
public class Solution228 {

    public List<String> summaryRanges(int[] nums) {
        boolean continueFlag = false;
        List<String> strList = new ArrayList<>();
        StringBuffer str = null;
        for (int i = 0; i < nums.length; i++) {
            if (i >= 1 && nums[i] == (nums[i - 1] + 1)) {
                //连续空间
                //最后一个如果是连续的，需要续上
                if (i == nums.length - 1) {
                    str.append("->" + nums[i]);
                    strList.add(str.toString());
                    str = null;
                }
                continue;
            } else {
                //发生断续
                if (i - 1 >= 0) {
                    if (Integer.parseInt(str.toString()) != nums[i - 1]) {
                        str.append("->" + nums[i - 1]);
                        strList.add(str.toString());
                    } else {
                        strList.add(str.toString());
                    }
                }
                str = new StringBuffer(String.valueOf(nums[i]));
            }
        }
        //最后一个连续
        if (str != null) {
            strList.add(str.toString());
        }
        return strList;
    }

    public static void main(String[] args) {
//        int[] arr = {0, 2, 3, 4, 6, 8, 9};
        int[] arr = {0, 1, 2, 4, 5, 7};
        Solution228 solution228 = new Solution228();
        List<String> resList = solution228.summaryRanges(arr);
        for (String str : resList) {
            System.out.println(str);
        }
    }

}
